package com.zhongkeruan.user.jpa;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.zhongkeruan.user.vo.User;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
	/**
	 * 根据用户名查找用户信息
	 * 
	 * @param username
	 * @return User
	 */
	public User findByUsername(String username) throws Exception;

	/**
	 * 查询用户
	 */
	public List<User> findAll();

	/**
	 * 根据id查询用户
	 */
	public Optional<User> findById(Long id);

	/**
	 * 保存用户
	 */
	@SuppressWarnings("unchecked")
	public User save(User user);

	/**
	 * 删除用户
	 */
	@Modifying
	@Query(value = "delete from t_user where id=?1", nativeQuery = true)
	public void delete(@Param("id") Long id) throws Exception;

	/**
	 * 更新用户
	 */
	@Modifying
	@Query(value="update t_user u set u.username=?2 where id =?1",nativeQuery=true)
	public void update(@Param("id") Long id, @Param("userName") String userName) throws Exception;
}
